package com.gxa.scdx.cloud.user.util;

import com.gxa.scdx.cloud.user.service.impl.DataServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;


//@author 颜可翔

@Component
public class TimeTask {

    @Autowired
    private DataServiceImpl dataService;

    /**
     * 每四秒执行一次
     */
    @Scheduled(fixedDelay = 4000L)
    public void task(){
        try {
            // 指定一个端口
            Integer port = 9999;
            ServerSocket serverSocket = new ServerSocket(port);

            System.out.println("socket服务注册成功...");
            System.out.println("等待客户端的连接...");

            while (true){
                // 监听 => 阻塞
                Socket accept = serverSocket.accept();
                // 创建并启动线程
                new ServerThread(accept, dataService).start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
